package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
        @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
        private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Override
    public void sumbit(Orders orders) {
            //获得当前用户id
        Long currentId = BaseContext.getCurrentId();
        //查询购物车中的数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        if (shoppingCartList ==null || shoppingCartList.size()==0){
            throw new CustomException("购物车为空");
        }
        //查询用户数据
        User byId = userService.getById(currentId);
        //查询用户地址
        Long addressBookId = orders.getAddressBookId();
        AddressBook byId1 = addressBookService.getById(addressBookId);
        if (byId1 == null){
            throw new CustomException("地址不能为空");
        }
        //订单号
        long id = IdWorker.getId();//获取订单号
        AtomicInteger atomicInteger = new AtomicInteger(0);
        //订单明细 信息
        ArrayList<OrderDetail> orderDetailArrayList = new ArrayList<>();
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(shoppingCart,orderDetail);
            orderDetail.setOrderId(id);
            orderDetailArrayList.add(orderDetail);
            BigDecimal amount = shoppingCart.getAmount();
            Integer number = shoppingCart.getNumber();
            BigDecimal multiply = amount.multiply(new BigDecimal(number));
            atomicInteger.addAndGet(multiply.intValue());
        }
        //订单信息
        orders.setId(id);
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setOrderTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(atomicInteger.get()));
        orders.setUserId(currentId);
        orders.setNumber(String.valueOf(id));
        orders.setUserName(byId.getName());
        orders.setConsignee(byId1.getConsignee());
        orders.setPhone(byId1.getPhone());
            orders.setAddress((byId1.getProvinceName() == null ? "" : byId1.getProvinceName())
                + (byId1.getCityName() == null ? "" : byId1.getCityName())
                + (byId1.getDistrictName() == null ? "" : byId1.getDistrictName())
                + (byId1.getDetail() == null ? "" : byId1.getDetail()));

        //计算价格    计算单价 * 数量

        //向订单中添加数据
        orderService.save(orders);
        orderDetailService.saveBatch(orderDetailArrayList);
        //清空购物车
        shoppingCartService.remove(lqw);

    }


}
